/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Trazabilidad.java
 *
 * Created on 22/08/2012, 06:11:51 PM
 */
package v;

import core.bd;
import core.constructor.constructor;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ButtonModel;
import javax.swing.DefaultListModel;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;
import tools.DatosComboBox;
import tools.Util;

/**
 *
 * @author !gaso
 */
public class Trazabilidad extends javax.swing.JPanel {

    constructor c;
    DatosComboBox cbx;
    //Modelo de DAtos del JList Discapacidades
    DefaultListModel modelDiscapacidad = new DefaultListModel();
    //Modelo de Datos del JList Medicemtno / Terapia
    DefaultListModel modelElemento = new DefaultListModel();
    private String idDiscapacidad = "";
    private String tipoTratamiento = "";
    private String idTerapia = "";
    private String idMedicamento = "";
    private String idpaciente = "";

    public Trazabilidad(Object... e) {
        initComponents();
        config(e);
        //Inicializar Fechas
        configFechas();
        bTrazabilidad.setEnabled(false);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        gEvolucion = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jXTitledSeparator1 = new org.jdesktop.swingx.JXTitledSeparator();
        jLabel1 = new javax.swing.JLabel();
        cNombrePaci = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        cEdad = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        lDiscapacidad = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        lElemento = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        lTratamiento = new javax.swing.JList();
        bTrazabilidad = new javax.swing.JButton();
        jXTitledSeparator2 = new org.jdesktop.swingx.JXTitledSeparator();
        jLabel3 = new javax.swing.JLabel();
        cFechaInicio = new org.jdesktop.swingx.JXDatePicker();
        cFechaFin = new org.jdesktop.swingx.JXDatePicker();
        jLabel4 = new javax.swing.JLabel();
        jXTitledSeparator3 = new org.jdesktop.swingx.JXTitledSeparator();
        evolu_all = new javax.swing.JRadioButton();
        evolu_0 = new javax.swing.JRadioButton();
        evolu_1 = new javax.swing.JRadioButton();
        evolu_2 = new javax.swing.JRadioButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        cTelefono = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        cFecha = new javax.swing.JTextField();
        cEstado = new javax.swing.JTextField();
        jScrollPane4 = new javax.swing.JScrollPane();
        cNotas = new javax.swing.JTextArea();
        jLabel9 = new javax.swing.JLabel();
        jXTitledSeparator4 = new org.jdesktop.swingx.JXTitledSeparator();

        addVetoableChangeListener(new java.beans.VetoableChangeListener() {
            public void vetoableChange(java.beans.PropertyChangeEvent evt)throws java.beans.PropertyVetoException {
                formVetoableChange(evt);
            }
        });

        jXTitledSeparator1.setFont(new java.awt.Font("Tahoma", 1, 15)); // NOI18N
        jXTitledSeparator1.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);
        jXTitledSeparator1.setTitle("Datos Paciente");

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setText("Nombres");

        cNombrePaci.setEditable(false);
        cNombrePaci.setColumns(1);
        cNombrePaci.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        cNombrePaci.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("Estado");

        cEdad.setEditable(false);
        cEdad.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        cEdad.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        lDiscapacidad.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lDiscapacidad.setSelectedIndex(0);
        lDiscapacidad.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lDiscapacidadValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(lDiscapacidad);

        lElemento.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lElementoValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(lElemento);

        lTratamiento.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Terapias", "Medicamentos" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lTratamiento.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lTratamiento.setSelectedIndex(0);
        lTratamiento.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lTratamientoValueChanged(evt);
            }
        });
        jScrollPane3.setViewportView(lTratamiento);

        bTrazabilidad.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        bTrazabilidad.setIcon(new javax.swing.ImageIcon(getClass().getResource("/core/img/trazabilidad32.png"))); // NOI18N
        bTrazabilidad.setText("Generar Trazabilidad");
        bTrazabilidad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bTrazabilidadActionPerformed(evt);
            }
        });

        jXTitledSeparator2.setFont(new java.awt.Font("Tahoma", 1, 15)); // NOI18N
        jXTitledSeparator2.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);
        jXTitledSeparator2.setTitle("Tratamientos");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Fecha Inicio");

        cFechaInicio.setFormats("dd-MM-yyyy");

        cFechaFin.setFormats("dd-MM-yyyy");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setText("Fecha Fin");

        jXTitledSeparator3.setFont(new java.awt.Font("Tahoma", 1, 15)); // NOI18N
        jXTitledSeparator3.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);
        jXTitledSeparator3.setTitle("Filtrar por Evolución");

        gEvolucion.add(evolu_all);
        evolu_all.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        evolu_all.setSelected(true);
        evolu_all.setText("Todas");
        evolu_all.setName("evol_all"); // NOI18N

        gEvolucion.add(evolu_0);
        evolu_0.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        evolu_0.setText("Desfavorable");
        evolu_0.setName("evol_0"); // NOI18N

        gEvolucion.add(evolu_1);
        evolu_1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        evolu_1.setText("Estable");
        evolu_1.setName("evol_1"); // NOI18N

        gEvolucion.add(evolu_2);
        evolu_2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        evolu_2.setText("Satisfactoria");
        evolu_2.setName("evol_2"); // NOI18N

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/core/img/busqueda32.png"))); // NOI18N
        jLabel5.setToolTipText("Buscar Paciente");
        jLabel5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel5MouseClicked(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel6.setText("Fecha");
        jLabel6.setToolTipText("Fecha Evaluación");

        cTelefono.setEditable(false);
        cTelefono.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        cTelefono.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel7.setText("Edad");

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel8.setText("Notas");

        cFecha.setEditable(false);
        cFecha.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        cFecha.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        cFecha.setToolTipText("Fecha Evaluación");

        cEstado.setEditable(false);
        cEstado.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        cEstado.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jScrollPane4.setEnabled(false);

        cNotas.setEditable(false);
        cNotas.setColumns(20);
        cNotas.setLineWrap(true);
        cNotas.setRows(3);
        cNotas.setEnabled(false);
        jScrollPane4.setViewportView(cNotas);

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel9.setText("Telefono");

        jXTitledSeparator4.setFont(new java.awt.Font("Tahoma", 1, 15)); // NOI18N
        jXTitledSeparator4.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);
        jXTitledSeparator4.setTitle("Filtrar por Fechas");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 496, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jXTitledSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 470, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(2, 2, 2)
                            .addComponent(cNombrePaci, javax.swing.GroupLayout.PREFERRED_SIZE, 340, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(10, 10, 10)
                            .addComponent(jLabel5))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(2, 2, 2)
                            .addComponent(cEdad, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(10, 10, 10)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addGap(60, 60, 60)
                                    .addComponent(cEstado, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(10, 10, 10)
                            .addComponent(cTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(10, 10, 10)
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(10, 10, 10)
                            .addComponent(cFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(10, 10, 10)
                            .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 380, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jXTitledSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 467, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(20, 20, 20)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(16, 16, 16)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(21, 21, 21)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jXTitledSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 467, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(20, 20, 20)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(2, 2, 2)
                            .addComponent(cFechaInicio, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(15, 15, 15)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(12, 12, 12)
                            .addComponent(cFechaFin, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jXTitledSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 467, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(20, 20, 20)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addGap(110, 110, 110)
                                    .addComponent(evolu_0, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(evolu_all, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(19, 19, 19)
                            .addComponent(evolu_1, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(10, 10, 10)
                            .addComponent(evolu_2, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(141, 141, 141)
                            .addComponent(bTrazabilidad, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 591, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jXTitledSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(14, 14, 14)
                            .addComponent(jLabel1))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(10, 10, 10)
                            .addComponent(cNombrePaci, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(5, 5, 5)
                            .addComponent(jLabel7))
                        .addComponent(cEdad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(cEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(7, 7, 7)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(cTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(cFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(10, 10, 10)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel9)
                                .addComponent(jLabel6))))
                    .addGap(13, 13, 13)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel8)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(10, 10, 10)
                    .addComponent(jXTitledSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(15, 15, 15)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(20, 20, 20)
                    .addComponent(jXTitledSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(5, 5, 5)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel3)
                        .addComponent(cFechaInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4)
                        .addComponent(cFechaFin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addComponent(jXTitledSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(5, 5, 5)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(evolu_0)
                        .addComponent(evolu_all)
                        .addComponent(evolu_1)
                        .addComponent(evolu_2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(15, 15, 15)
                    .addComponent(bTrazabilidad, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void bTrazabilidadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bTrazabilidadActionPerformed

        if ((cFechaFin.getDate().after(cFechaInicio.getDate())) || cFechaFin.getDate().equals(cFechaInicio.getDate())) {
            ejecutarReporte(("ter".equals(tipoTratamiento)) ? idTerapia  : idMedicamento);
            System.out.println("Util.valorRadioButton(gEvolucion) = " + Util.valorRadioButton(gEvolucion));
        } else {
            Logger.getLogger(Trazabilidad.class.getName()).log(Level.WARNING, "Las Fechas No concuerdan");
            c.infoUsuario("error", "La <strong>Fecha de Fin</strong> no puede ser inferior a la <strong>Fecha Inicio</strong>", "Rango de Fechas Incorrectas");
        }


    }//GEN-LAST:event_bTrazabilidadActionPerformed

    private void lDiscapacidadValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lDiscapacidadValueChanged

        if (evt.getValueIsAdjusting()) {
            // idDiscapacidad = cbx.getKeyDiscapacidad(lDiscapacidad);
            cambiosListas();
        }
    }//GEN-LAST:event_lDiscapacidadValueChanged

    private void lTratamientoValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lTratamientoValueChanged
        if (evt.getValueIsAdjusting()) {
            cambiosListas();
//            modelElemento.clear();
//            tipoTratamiento = lTratamiento.getSelectedValue().toString().substring(0, 3).toLowerCase();
//            
//            if (tipoTratamiento.equals("ter")) {
//                String[] lTerapiasPac = cbx.lTerapiasPac("1", idDiscapacidad);
//                for (int i = 0; i < lTerapiasPac.length; i++) {
//                    modelElemento.add(i, lTerapiasPac[i]);
//                }
//                lElemento.setModel(modelElemento);
//                idTerapia = cbx.getKeyTerapiasPac(lElemento);
//            } else if (tipoTratamiento.equals("med")) {
//                String[] lMedicamentosPac = cbx.lMedicamentosPac("1", idDiscapacidad);
//                for (int i = 0; i < lMedicamentosPac.length; i++) {
//                    modelElemento.add(i, lMedicamentosPac[i]);
//                }
//                lElemento.setModel(modelElemento);
//                idMedicamento = cbx.getKeyMedicamentosPac(lElemento);
//            }
        }
    }//GEN-LAST:event_lTratamientoValueChanged

    private void jLabel5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel5MouseClicked
        SeleccionadorIU select = new SeleccionadorIU("SELECT pac.id_paciente, pac.identificacion , UPPER(pac.nombres) ||' '|| UPPER(pac.apellidos) ,  EXTRACT(YEAR FROM age(pac.fecha_nacimiento))::text||' Años' , acu.telefono , est.nombre , to_char(tra.fecha_evaluacion, 'dd-MM-yyyy'), tra.nota as NOTA  FROM paciente pac , estado_paciente est , tratamiento tra , acudiente acu "
                + "WHERE pac.estado = est.id_estado AND pac.id_paciente = tra.id_paciente AND pac.id_acudiente = acu.id_acudiente "
                + "ORDER BY pac.estado DESC , tra.fecha_evaluacion DESC", "ID,IDENTIFICACIÓN,PACIENTE,EDAD,TELEFONO,ESTADO,FECHA EVALUACION,NOTAS", c, this.getClass().getName(), "cargar");

        select.setTitle("Seleccione Paciente");
        select.setVisible(true);
        Map<String, String> datos = select.getDatosSelect();
        if (!datos.isEmpty()) {
            cargar(datos);

        }
    }//GEN-LAST:event_jLabel5MouseClicked

    private void formVetoableChange(java.beans.PropertyChangeEvent evt)throws java.beans.PropertyVetoException {//GEN-FIRST:event_formVetoableChange
        // TODO add your handling code here:
    }//GEN-LAST:event_formVetoableChange

    private void lElementoValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lElementoValueChanged
//        if (lElemento.getSelectedValue() != null ){
//             System.out.println("lElemento =  " + lElemento.getSelectedValue());
//            idTerapia = lElemento.getSelectedValue().toString();
//        }
    }//GEN-LAST:event_lElementoValueChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bTrazabilidad;
    private javax.swing.JTextField cEdad;
    private javax.swing.JTextField cEstado;
    private javax.swing.JTextField cFecha;
    private org.jdesktop.swingx.JXDatePicker cFechaFin;
    private org.jdesktop.swingx.JXDatePicker cFechaInicio;
    private javax.swing.JTextField cNombrePaci;
    private javax.swing.JTextArea cNotas;
    private javax.swing.JTextField cTelefono;
    private javax.swing.JRadioButton evolu_0;
    private javax.swing.JRadioButton evolu_1;
    private javax.swing.JRadioButton evolu_2;
    private javax.swing.JRadioButton evolu_all;
    private javax.swing.ButtonGroup gEvolucion;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private org.jdesktop.swingx.JXTitledSeparator jXTitledSeparator1;
    private org.jdesktop.swingx.JXTitledSeparator jXTitledSeparator2;
    private org.jdesktop.swingx.JXTitledSeparator jXTitledSeparator3;
    private org.jdesktop.swingx.JXTitledSeparator jXTitledSeparator4;
    private javax.swing.JList lDiscapacidad;
    private javax.swing.JList lElemento;
    private javax.swing.JList lTratamiento;
    // End of variables declaration//GEN-END:variables

    private void config(Object... e) {
        if (e.length > 0) {
            try {
                c = (constructor) e[0];

//                //Configura la Interfaz para el Modo en el que fue invocado
//                configModo((String) e[1], (String) e[2]);


            } catch (Exception exc) {
                System.err.println("(config())Error de Asignacion , compruebe el numero de Parametros que envia .. " + exc.getMessage());
                exc.printStackTrace();
            }
        }

        // Indicar a las 'migas de pan' donde estamos.
        c.setLabelModulo("Reporte");
        c.setLabelAccion("Trazabilidad Tratamiento");

        cbx = new DatosComboBox();

    }

    /**
     * 
     * @param id terapia o medicamento
     */
    
    public void ejecutarReporte(String id) {

        try {
            String archiJasper = "";
            JasperReport masterReport = null;
            String tipo = ("ter".equals(tipoTratamiento)) ? "Terapias"  : "Medicamentos";
            try {
                if ("ter".equals(tipoTratamiento)) {
                    archiJasper = "tra_terapias.jasper";
                } else if ("med".equals(tipoTratamiento)) {
                    archiJasper = "tra_medic.jasper";
                }
                masterReport = (JasperReport) JRLoader.loadObject(getClass().getResource("/reportes/".concat(archiJasper)));
            } catch (JRException e) {
                System.out.println("Error cargando el reporte maestro: " + e.getMessage());
            }

            //Config Parametros


            //este es el parámetro, se pueden agregar más parámetros
            //basta con poner mas parametro.put
            Map parametro = new HashMap();
            parametro.put( ("ter".equals(tipoTratamiento)) ? "id_terapia" : "id_medicamento", Integer.valueOf(id));
            parametro.put("id_paciente", Integer.valueOf(idpaciente));
            parametro.put("evol", filtroEvolucion());
            parametro.put("fechaInicio", Util.dateToTimestamp(cFechaInicio.getDate()));
            parametro.put("fechaFin", Util.dateToTimestamp(cFechaFin.getDate()));
            parametro.put("LOGO", getClass().getResource("/reportes/LogoFundacion.png").toString());

            //Reporte diseñado y compilado con iReport
            JasperPrint jasperPrint = JasperFillManager.fillReport(masterReport, parametro, bd.getConexion());

            //Se lanza el Viewer de Jasper, no termina aplicación al salir
            JasperViewer jviewer = new JasperViewer(jasperPrint, false);
            jviewer.setTitle(String.format("Fundacion Superar - Trazabilidad (%s)", tipo));
            jviewer.setVisible(true);
        } catch (JRException e) {
            System.out.println("Error generando reporte " + e.getMessage());
            e.printStackTrace();
        }
    }

    void cambiosListas() {
        modelElemento.clear();
        idDiscapacidad = cbx.getKeyDiscapacidad(lDiscapacidad);
        tipoTratamiento = lTratamiento.getSelectedValue().toString().substring(0, 3).toLowerCase();

        if ("ter".equals(tipoTratamiento)) {
            String[] lTerapiasPac = cbx.lTerapiasPac(idpaciente, idDiscapacidad);
            for (int i = 0; i < lTerapiasPac.length; i++) {
                modelElemento.add(i, lTerapiasPac[i]);
            }
            lElemento.setModel(modelElemento);

            lElemento.requestFocus();
            lElemento.grabFocus();

            if (!modelElemento.isEmpty()) {
                lElemento.setSelectedIndex(0);
            }

            idTerapia = cbx.getKeyTerapiasPac(lElemento);

            System.out.println("idTerapia = " + idTerapia);
        } else if ("med".equals(tipoTratamiento)) {
            String[] lMedicamentosPac = cbx.lMedicamentosPac(idpaciente, idDiscapacidad);
            for (int i = 0; i < lMedicamentosPac.length; i++) {
                modelElemento.add(i, lMedicamentosPac[i]);
            }
            lElemento.setModel(modelElemento);
            lElemento.requestFocus();
            lElemento.grabFocus();

            if (!modelElemento.isEmpty()) {
                lElemento.setSelectedIndex(0);
            }

            idMedicamento = cbx.getKeyMedicamentosPac(lElemento);
            
            System.out.println("idMedicamento = " + idMedicamento);
        }
    }

    void cargarDiscapacidades() {
        //Limpiar Listas Anteriores
        modelDiscapacidad.clear();

        String[] lDiscapacidades = cbx.lDiscapacidades(idpaciente);

        for (int i = 0; i < lDiscapacidades.length; i++) {
            modelDiscapacidad.add(i, lDiscapacidades[i]);
        }
        lDiscapacidad.setModel(modelDiscapacidad);

        if (lDiscapacidad.getModel().getSize() != 0) {
            //Seleccionar Primer Elemento de la Lista
            lDiscapacidad.setSelectedIndex(0);
            //Enviar valor al id_discapacidad
            idDiscapacidad = cbx.getKeyDiscapacidad(lDiscapacidad);
            //Condicionar Tipo de Tratamiento
            tipoTratamiento = lTratamiento.getSelectedValue().toString().substring(0, 3).toLowerCase();
        }
    }

    private void cargar(Map<String, String> datosSelect) {

        //IDENTIFICACIÓN,PACIENTE,EDAD,ESTADO,FECHA EVALUACION
        idpaciente = datosSelect.get("ID");
        cNombrePaci.setText(datosSelect.get("PACIENTE"));
        cEdad.setText(datosSelect.get("EDAD").equals("null") ? "-NA-" : datosSelect.get("EDAD"));
        cEstado.setText(datosSelect.get("ESTADO"));
        cFecha.setText(datosSelect.get("FECHA EVALUACION"));
        cTelefono.setText(datosSelect.get("TELEFONO"));
        cNotas.setText(datosSelect.get("NOTAS"));

        //Cargar Listas con Seleccion
        cargarDiscapacidades();
        cambiosListas();

        //Habilitar Boton Generar PDF
        if (!idpaciente.isEmpty()) {
            bTrazabilidad.setEnabled(true);
        }

    }

    private void configFechas() {
        //Obtener Fecha Actual
        Calendar c = Calendar.getInstance();

        // Colocar Fecha de Inicio el primer dia del Mes actual.
        c.set(Calendar.DATE, c.getActualMinimum(Calendar.DATE));
        cFechaInicio.setDate(c.getTime());

        //Colocar Fecha de Fin segun el ultimo dia del Mes actual
        c.set(Calendar.DATE, c.getActualMaximum(Calendar.DATE));
        cFechaFin.setDate(c.getTime());

    }

    private java.util.Collection filtroEvolucion() {//evol_

        java.util.Collection listaCalificacion = new ArrayList();
        String valorRadioButton = Util.valorRadioButton(gEvolucion).substring(5);
        System.out.println("valorRadioButton = " + valorRadioButton);

        if (valorRadioButton.equals("all")) {
            listaCalificacion.add(0);
            listaCalificacion.add(1);
            listaCalificacion.add(2);
        } else {
            listaCalificacion.add(Integer.valueOf(valorRadioButton));
        }

        return listaCalificacion;
    }
}
